const express = require('express'); //引入express模块
const db = require('../db'); //引入user模块

//路由中间件
const router = express.Router();
module.exports = router;

//注册

router.post('/', async (req, res) => {
    const {
        name,
        password
    } = req.body
    
    const sql = `insert into user
    (name,password) values('${name}','${password}')`

    const data = await db(sql);

    if(data.insertId){
        //统一前后端数据格式
        res.send({
            code:200
        })
    }else{
        res.send({
            code:400
        })
    }
});


router.get('/check', async (req, res) => {
    const {
        name
    } = req.query;
    const sql = `select name from user where name='${name}'`
    console.log('heresql=', sql)
    const data = await db(sql);
    if (data.length > 0) {
        //res.send('用户已存在')
        res.header('Access-Control-Allow-Origin',"*")
        res.send({
            code:400
        })
    } else {
        //res.send('可注册')
        res.send({
            code:200
        })
    }
});